﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{

    public class JLTGTFData : BaseFunction
    {
        
        public JLTGTFData()
            : base()
        {

        }
        public JLTGTFData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetGTF(DataRow dwABPOS)
        {
            DataSet Result;
            DataSet Return = new DataSet();
            string TableName = "GTF";
            string ART = dwABPOS["ART"].ToString().Trim();
            string LB = dwABPOS["LB"].ToString().Trim();
            string STAFF = dwABPOS["STAFF"].ToString().Trim();
            string FROMDNDATE = dwABPOS["FROMDNDATE"].ToString().Trim();
            string ENDDNDATE = dwABPOS["ENDDNDATE"].ToString().Trim();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT TRIM(A.WBEWART) AS ART,TRIM(A.WBEWFARBE) AS COLOR, "
                    + " A.WBEWBUCHDAT,A.WBEWBUCHBEN AS USERID,A.WBEWMPOS AS MACHINE, "
                    + " (CASE WHEN B.WBBIBRT = B.WBBIBRTFV THEN ' ' ELSE TO_CHAR(B.WBBIBRTFV* 0.001) END) AS GMTR, "
                    + " DECODE(B.WBBDM,0,' ',B.WBBDM) AS LOOM,B.WBBFARBN AS STAFF,WBBMAART,DECODE(WBBAUSFGRI,100,'',WBBAUSFGRI) AS WBBAUSFGRI,WBBSTKNR, "
                    + " A.WBEWMTR*0.001 AS MTR,ROUND(A.WBEWMTR/0.9144*0.001,1) AS QTY,A.WBEWLB,B.WBBTAX,to_number(b.WBBPACKNR) as WBBPACKNR  "
                    + " FROM WBBEW A ,WBBST B "
                    + " WHERE A.WBEWMANDANT = B.WBBMANDANT AND A.WBEWRWKRS = B.WBBRWKRS "
                    + " AND A.WBEWRWLFD = B.WBBRWLFD AND A.WBEWPOS = B.WBBPOS "
                    + " AND A.WBEWBS ='13' AND A.WBEWWAKZ = 'F' and a.wbewmtr>3000 "
                    + " AND (A.WBEWBUCHDAT >='" + FROMDNDATE + "' AND A.WBEWBUCHDAT <='" + ENDDNDATE + "') "
                    + " AND A.WBEWART LIKE '%" + ART + "%' "
                    + " AND (TRIM(A.WBEWBUCHBEN) = '" + STAFF + "' OR '" + STAFF + "' = 'AAAAA' ) "
                    + " AND (A.WBEWLB = '" + LB + "' OR '" + LB + "' = 'AAA' ) "
                    + " ORDER BY 1,2,WBBPACKNR,3   "
                    ;

                Result = Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    //byte[] b;
                    //string value;
                    //foreach (DataRow dw in Result.Tables[TableName].Rows)
                    //{
                    //    b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["BEM"].ToString().Trim());
                    //    value = Encoding.GetEncoding("gb2312").GetString(b);
                    //    dw["BEM"] = value;
                    //    b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["DES"].ToString().Trim());
                    //    value = Encoding.GetEncoding("gb2312").GetString(b);
                    //    dw["DES"] = value;
                    //}
                }
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }       
        public DataSet BuildJLTGTFData(DataRow dwUI)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                setResult = GetGTF(dwUI);
                setReturn.Merge(setResult);                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}